import os
from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise

from auth import router as auth_router
from home import router as home_router

app = FastAPI(
    title='FastAPI JWT Auth',
    description='FastAPI实现JWT登录认证功能',
    version='1.0.0',
    docs_url='/docs',
    redoc_url='/redocs'
)

app.include_router(auth_router, prefix='/auth', tags=['Auth登录认证'])
app.include_router(home_router, tags=['Home主页信息'])

register_tortoise(
    app,
    # db_url=os.environ.get('TEST_DB', 'sqlite://:memory:'),  # 测试时使用内存数据库
    db_url='mysql://root:root@localhost:3306/test03',  # 需要手动创建数据库 create database test01;
    modules={'models': ['auth.models']},
    generate_schemas=True,  # 生成表结构,只会创建一次
    add_exception_handlers=True
)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run('run:app', debug=True, reload=True)
